<?php

namespace App\Api\Controllers\Device;


use App\Api\Controllers\BaseController;
use App\Models\Device;
use App\Models\Merchant;
use App\Models\MerchantStore;
use App\Models\Store;
use App\Models\VConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use MyBank\Tools;

class DeviceController extends BaseController
{

    //添加设备
    public function add(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();
            $store_id = $request->get('store_id');
            $merchant_id = $request->input('merchant_id', "");
            $device_type = $request->get('device_type', '');
            $device_name = $request->get('device_name', '');
            $device_no = $request->get('device_no', '');
            $device_key = $request->get('device_key', '');
            $type = $request->get('type', '');
            $ascription = $request->get('ascription', '') ? $request->get('ascription', '') : 0;
            $cj = $request->get('cj', '');
            $cj = isset($cj) ? $cj : "";
            $config_id = $user_merchant->config_id;
            $data = $request->except(['token']);

            if (in_array($type, ['p'])) {
                $check_data = [
                    'store_id' => '门店id',
                    'device_type' => '设备类型',
                    'device_no' => '设备编号',
                    'device_key' => '设备秘钥',
                    'device_name' => '设备名称'
                ];
            } else {
                $check_data = [
                    'store_id' => '门店id',
                    'device_type' => '设备类型',
                    'device_no' => '设备编号',
                    'device_key' => '设备秘钥',
                    'device_name' => '设备名称'
                ];
            }
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $device_repeat = Device::where('device_no', $device_no)->first();
            if (!empty($device_repeat)) {
                return json_encode([
                    'status' => 2,
                    'message' => '该设备已被绑定'
                ]);
            }

            $merchant_name = "";

            if ($merchant_id == "") {
                $merchant_id = "";
            }

            $store = Store::where('store_id', $store_id)
                ->select('store_name', 'ascription')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }

            $Device = Device::where('device_no', $device_no)
                ->where('device_type', $device_type)
                ->where('store_id', $store_id)
                ->first();
//            if ($Device && in_array($type, ['s', 'f'])) {
            if ($Device) {
                return json_encode([
                    'status' => 2,
                    'message' => '设备已经被' . $Device->store_name . '绑定',
                ]);
            }

            $store_name = $store->store_name;

            $VConfig = VConfig::where('config_id', $config_id)
                ->select(
                    'zw_token',
                    'zlbz_token',
                    'yly_user_name',
                    'yly_user_id',
                    'yly_api_key'
                )->first();
            if (!$VConfig) {
                $VConfig = VConfig::where('config_id', '1234')
                    ->select(
                        'zw_token',
                        'zlbz_token',
                        'yly_user_name',
                        'yly_user_id',
                        'yly_api_key'
                    )->first();
            }
            if (!$VConfig) {
                return json_encode([
                    'status' => 2,
                    'message' => '系统配置-设备配置，参数未设置'
                ]);
            }


            // $pash = $request->path();
            // $pass = $request->get('pass', '');
            // $pass = substr(base64_decode($pass), 0, -5);

            // $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证
            // $pass_verify = json_decode($pass_verify, true);
            // // print_r( $request );die;
            // if( $pass_verify['status'] == '1'){
            //     $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
            //     $remark = '门店添加设备: 门店id: ' . $store_id . '添加设备';
            //     $this->operation_log( $this->parseToken()->user_id, 9, $remark , $pass_id );
            // }else{
            //     return json_encode(['status' => 2, 'message' => '验证不通过', 'data' => []]);
            // }


            //云喇叭添加-智联
            if ($device_type == 'v_zlbz_1') {
                //强制解绑
                $datadel = [
                    'id' => $device_no,
                    'm' => '4',
                    'token' => $VConfig->zlbz_token
                ];
                $curl = Tools::curl($datadel, 'http://39.106.131.149/bind.php');
                $curl = json_decode($curl, true);
                //绑定
                $datap = [
                    'id' => $device_no,
                    'm' => '1',
                    'uid' => $store_id,
                    'token' => $VConfig->zlbz_token
                ];

                $curl = Tools::curl($datap, 'http://39.106.131.149/bind.php');
                $curl = json_decode($curl, true);
                if ($curl['errcode'] != 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $curl['errcode'] . '-' . $curl['errmsg']
                    ]);
                }
            }

            //v_zw_1 智网云喇叭
            if ($device_type == 'v_zw_1') {
                //1.强制解绑
                $url = "http://cloudspeaker.smartlinkall.com/bind.php?id=" . $device_no . "&m=4&uid=" . $store_id . $device_no . "&token=" . $VConfig->zw_token . "&seq=" . date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
                $data = Tools::curl_get($url);

                //2.绑定
                $url = "http://cloudspeaker.smartlinkall.com/bind.php?id=" . $device_no . "&m=1&uid=" . $store_id . $device_no . "&token=" . $VConfig->zw_token . "&seq=" . date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
                $data = Tools::curl_get($url);
                $data = json_decode($data, true);
                if ($data['errcode'] != 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $data['errmsg']
                    ]);
                }
            }

            //易联云打印机 k4
            if ($device_type == 'p_yly_k4') {
                $push = new YlianyunAopClient();
                $push_id = $VConfig->yly_user_id; //用户id
                $push_key = $VConfig->yly_api_key; //api密钥
                $push_user_name = $VConfig->yly_user_name; //用户名
                $print_key = $device_key;
                $print_name = $device_no;

                $add = $push->action_addprint($push_id, $print_name, $push_user_name, $print_name, '1', $push_key, $print_key);
                if ($add != '1') {
                    $msg = '添加失败';
                    if ($add == '2') {
                        $msg = '不要重复添加打印机';
                    }
                    return json_encode([
                        'status' => 2,
                        'message' => $msg
                    ]);
                }
            }
            //飞鹅云打印机 p_fey
            if ($device_type == 'p_fey') {
                $time = time();         //请求时间
                $printerContent = $device_no . "#" . $device_key;
                $msgInfo = array(
                    'user' => '2067925302@qq.com',
                    'stime' => $time,
                    'sig' => sha1('2067925302@qq.com' . '2bjzLB3PQpTPC65Z' . $time),
                    'apiname' => 'Open_printerAddlist',
                    'printerContent' => $printerContent
                );
                $client = $this->feieyun('https://api.feieyun.cn/Api/Open/', $msgInfo);
                $client = json_decode($client, true);
                if ($client['ret'] != '0') {
                    $msg = '添加失败';
                    return json_encode([
                        'status' => 2,
                        'message' => $msg
                    ]);
                }
            }

            if ($merchant_id) {
                $merchant_ids = explode(',', $merchant_id);
                foreach ($merchant_ids as $k => $v) {
                    $merchant_s = Merchant::where('id', $v)
                        ->select('name')
                        ->first();
                    if ($merchant_s) {
                        $merchant_name = $merchant_s->name;
                    } else {
                        continue;
                    }

                    $indata = [
                        'store_id' => $store_id,
                        'merchant_id' => $v,
                        'merchant_name' => $merchant_name,
                        'store_name' => $store_name,
                        'device_type' => $device_type,
                        'device_name' => $device_name,
                        'device_no' => $device_no,
                        'device_key' => $device_key,
                        'type' => $type,
                        'cj' => $cj,
                        'config_id' => $config_id,
                        'ascription' => $ascription
                    ];
                    Device::create($indata);
                }
            } else {
                $merchant_s = Merchant::where('id', $merchant_id)
                    ->select('name')
                    ->first();
                if ($merchant_s) {
                    $merchant_name = $merchant_s->name;
                }

                $indata = [
                    'store_id' => $store_id,
                    'merchant_id' => $merchant_id,
                    'merchant_name' => $merchant_name,
                    'store_name' => $store_name,
                    'device_type' => $device_type,
                    'device_name' => $device_name,
                    'device_no' => $device_no,
                    'device_key' => $device_key,
                    'type' => $type,
                    'config_id' => $config_id,
                    'cj' => $cj,
                    'ascription' => $ascription
                ];
                Device::create($indata);
            }
            if ($store->ascription != $ascription) {
                Store::where('store_id', $store_id)->update(array('ascription' => $ascription));
            }

            $this->status = 1;
            $this->message = "添加成功";
            return $this->format($data);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage()
            ]);
        }
    }


    public function up(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();
            $id = $request->get('id');
            $merchant_id = $request->get('merchant_id', '');
            $store_id = $request->get('store_id', '');
            $config_id = $user_merchant->config_id;
            $data = $request->except(['token']);
            $ascription = $request->post('ascription', '') ? $request->post('ascription', '') : 0;
            $cj = isset($data['cj']) ? $data['cj'] : "";
            $data['cj'] = $cj;

            $check_data = [
                'id' => '设备id',
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            if ($merchant_id == "" || $merchant_id == "NULL") {
                $merchant_id = "";
            }

            $merchant_name = "";
            if ($merchant_id) {
                $merchant_s = Merchant::where('id', $merchant_id)->first();
                if ($merchant_s) {
                    $merchant_name = $merchant_s->name;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '收银员不存在'
                    ]);
                }
            }

            $store = Store::where('store_id', $store_id)
                ->select('store_name', 'ascription')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }

            $merchant_ids = explode(',', $merchant_id);
            if (count($merchant_ids) > 1) {
                return json_encode([
                    'status' => 2,
                    'message' => '修改不支持添加多个'
                ]);
            }


            $pash = $request->path();
            $pass = $request->get('pass', '');
            $pass = substr(base64_decode($pass), 0, -5);

            $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证
            $pass_verify = json_decode($pass_verify, true);
            // print_r( $this->parseToken() );die;
            if ($pass_verify['status'] == '1') {
                $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
                // print_r( $data );die;
                $remark = '门店修改设备: 门店id: ' . $store_id . '修改设备id:' . $id;
                $this->operation_log($this->parseToken()->user_id, 10, $remark, $pass_id);
            } else {
                return json_encode(['status' => 2, 'message' => '验证不通过', 'data' => []]);
            }
            unset($data['pass']);
            unset($data['path']);


            $store_name = $store->store_name;
            $data['merchant_name'] = $merchant_name;
            $data['store_name'] = $store_name;
            $data['config_id'] = $config_id;
            $data['ascription'] = $ascription;
            Device::where('id', $id)->update($data);
            if ($store->ascription != $ascription) {
                Store::where('store_id', $store_id)->update(array('ascription' => $ascription));
            }
            $this->status = 1;
            $this->message = "修改成功";
            return $this->format($data);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage()
            ]);
        }
    }


    //删除设备
    public function del(Request $request)
    {

        if ($this->parseToken()->type == 'user' && $this->parseToken()->user_id == 1) {

        } else {
            $this->status = -1;
            $this->message = '暂无权限删除';
            return $this->format();
        }

        // $this->status = -1;
        // $this->message = '暂无权限删除';
        // return $this->format();

        try {
            $user_merchant = $this->parseToken();
            $id = $request->get('id');

            $data = $request->except(['token']);
            $check_data = [
                'id' => '设备id',
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $device = Device::where('id', $id)->first();
            if (!$device) {
                return json_encode([
                    'status' => 2,
                    'message' => '设备不存在'
                ]);
            }

            if ($user_merchant->type == "merchant") {
                return json_encode([
                    'status' => 2,
                    'message' => '暂无法解绑！请联系业务员'
                ]);
            }

            $VConfig = VConfig::where('config_id', $device->config_id)
                ->select(
                    'zw_token',
                    'zlbz_token',
                    'yly_user_id',
                    'yly_api_key'
                )->first();
            if (!$VConfig) {
                $VConfig = VConfig::where('config_id', '1234')
                    ->select(
                        'zw_token',
                        'zlbz_token',
                        'yly_user_id',
                        'yly_api_key'
                    )->first();
            }


            $pash = $request->path();
            $pass = $request->get('pass', '');
            $pass = substr(base64_decode($pass), 0, -5);

            $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证
            $pass_verify = json_decode($pass_verify, true);
            // print_r( $this->parseToken() );die;
            if ($pass_verify['status'] == '1') {
                $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
                $remark = '门店删除设备: 门店删除设备id: ' . $id;
                $this->operation_log($this->parseToken()->user_id, 11, $remark, $pass_id);
            } else {
                return json_encode(['status' => 2, 'message' => '验证不通过', 'data' => []]);
            }


            //云喇叭删除
            if ($device->device_type == 'v_zlbz_1') {
                $data = [
                    'id' => $device->device_no,
                    'm' => '0',
                    'uid' => $device->store_id,
                    'token' => $VConfig->zlbz_token
                ];
                $curl = Tools::curl($data, 'http://39.106.131.149/bind.php');
                $curl = json_decode($curl, true);
                if ($curl['errcode'] != 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $curl['errcode'] . '-' . $curl['errmsg']
                    ]);
                }
            }

            //易联云删除
            if ($device->device_type == "p_yly_k4") {
                $push = new YlianyunAopClient();
                $push_id = $VConfig->yly_user_id; //用户id
                $push_key = $VConfig->yly_api_key; //api密钥
                $print_key = $device->device_key;
                $print_name = $device->device_no;
                $delete = $push->action_removeprinter($push_id, $print_name, $push_key, $print_key);
                if ($delete != '1') {
                    $msg = '删除设备失败';
                    if ($delete == '2') {
                        $msg = '没这个设备';
                    }

                    return json_encode([
                        'status' => 2,
                        'message' => $msg
                    ]);
                }
            }

            Device::where('id', $id)->delete();

            $this->status = 1;
            $this->message = "删除成功";
            return $this->format($data);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage()
            ]);
        }
    }


    public function select(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();
            $id = $request->get('id');

            $data = $request->except(['token']);
            $check_data = [
                'id' => '设备id'
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $RE = Device::where('id', $id)->first();
            if (!$RE) {
                return json_encode([
                    'status' => 2,
                    'message' => 'id有误'
                ]);
            }

            $this->status = 1;
            $this->message = "查询成功";
            return $this->format($RE);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage()
            ]);
        }
    }


    public function lists(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $merchant_id = $request->get('merchant_id', '');
            $device_type = $request->get('device_type', '');
            $type = $request->get('type', '');

            $where = [];
            $whereIn = [];
            $store_ids = [];
            if ($merchant_id) {
                $where[] = ['merchant_id', '=', $merchant_id];
            }

            if ($store_id) {
                $store_ids = [
                    [
                        'store_id' => $store_id
                    ]
                ];
            } else {
                $MerchantStore = MerchantStore::where('merchant_id', $user_merchant->merchant_id)
                    ->select('store_id')
                    ->get();
                if (!$MerchantStore->isEmpty()) {
                    $store_ids = $MerchantStore->toArray();
                }
            }

            if ($device_type) {
                $where[] = ['device_type', '=', $device_type];
            }

            if ($type) {
                $where[] = ['type', '=', $type];
            }

            $obj = DB::table('devices');

            $data = $obj->where($where)
                ->whereIn('store_id', $store_ids)
                ->where("type","!=","pp")
                ->orderBy('updated_at', 'desc');
            $this->t = $data->count();
            $data = $this->page($data)->get();

            $this->status = 1;
            $this->message = "数据返回成功";
            return $this->format($data);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage()
            ]);
        }
    }


    public function device_type(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();
            $type = $request->get('type', 'p');

            if ($type == "p") {
                $data = [
                    [
                        'device_type' => 'p_zlbz_1',
                        'device_name' => '打印机(智联博众)'
                    ],
                    [
                        'device_type' => 'p_yly_k4',
                        'device_name' => '打印机(易联云)'
                    ],
                    [
                        'device_type' => 'p_fey',
                        'device_name' => '打印机(飞鹅云)'
                    ]
                ];
            }

            if ($type == "s") {
                $data = [
                    [
                        'device_type' => 's_bp_sl51',
                        'device_name' => '扫码盒子(sl51)'
                    ],
                    [
                        'device_type' => 's_nl_me50h',
                        'device_name' => '扫码盒子(ME50H)'
                    ],
                    [
                        'device_type' => 's_nl_me50c',
                        'device_name' => '扫码盒子(ME50C)'
                    ],
                    [
                        'device_type' => 's_bp_sl58',
                        'device_name' => '扫码设备(sl58)'
                    ],
                    [
                        'device_type' => 's_bp_sl56',
                        'device_name' => '扫码设备(sl56)'
                    ],
                    [
                        'device_type' => 's_ps_ys',
                        'device_name' => '扫码盒子(品生)'
                    ],
//                    [
//                        'device_type' => 'face_cm01',
//                        'device_name' => '刷脸设备(织点cm01)'
//                    ],
                    [
                        'device_type' => 's_bf_qr65',
                        'device_name' => '百富扫码王(qr65)'
                    ],
                    [
                        'device_type' => 's_bf_qr68',
                        'device_name' => '百富扫码王(qr68)'
                    ],
                    [
                        'device_type' => 's_bf_qr68_4g',
                        'device_name' => '百富扫码王(qr68-4G)'
                    ],
                    [
                        'device_type' => 's_ky_mp10',
                        'device_name' => '扫码盒子(mp10)'
                    ],
                    [
                        'device_type' => 's_s6',
                        'device_name' => '扫码盒子(s6)'
                    ],
                    [
                        'device_type' => 's_me70',
                        'device_name' => '新大陆(me70)'
                    ]
                ];
            }

            if ($type == "f") {
                $data = [
                    [
                        'device_type' => 'face_f4',
                        'device_name' => '支付宝蜻蜓'
                    ],
//		            [
//                        'device_type' => 'face_pro',
//                        'device_name' => '微信刷脸青蛙PRO'
//                    ],
                    [
                        'device_type' => 'face_TZH-L1',
                        'device_name' => '微信青蛙'
                    ],
//		            [
//                        'device_type' => 'face_cm01',
//                        'device_name' => '微信刷脸(织点cm01)'
//                    ],
//                    [
//                        'device_type' => 'face_jsd101',
//                        'device_name' => '微信刷脸(君时达Z2)'
//                    ]
                ];
            }

            if ($type == "l") {
                $data = [
                    [
                        'device_type' => 'APOS_A8',
                        'device_name' => '联迪A8'
                    ],
                ];
            }

            if ($type == "w") {
                $data = [
                    [
                        'device_type' => 'y_plugin_1',
                        'device_name' => '微收银'
                    ],
                ];
            }

            if ($type == "v") {
                $data = [
                    [
                        'device_type' => 'v_zlbz_1',
                        'device_name' => '播报设备(智联博众)'
                    ],
                    [
                        'device_type' => 'v_zw_1',
                        'device_name' => '播报设备(智网云)'
                    ],
                    [
                        'device_type' => 'v_bp_1',
                        'device_name' => '播报设备(波普SLX1)'
                    ],
                    [
                        'device_type' => 'v_kd_58',
                        'device_name' => '播报设备(新国都KD58)'
                    ],
                    [
                        'device_type' => 'v_jbp_d30',
                        'device_name' => '播报设备(聚宝盆D30)'
                    ],
                    [
                        'device_type' => 'v_ps_sp08',
                        'device_name' => '播报设备(品生sp08)'
                    ],
                    [
                        'device_type' => 'v_cls_q038',
                        'device_name' => '播报设备(畅立收Q038)'
                    ],
                    [
                        'device_type' => 'v_car_CG02',
                        'device_name' => '播报设备(云想印CG02)'
                    ],
                    [
                        'device_type' => 'v_you_pay',
                        'device_name' => '播报设备(邮储银行)'
                    ],
                    [
                        'device_type' => 'v_whty',
                        'device_name' => '播报设备(天喻)'
                    ],
                    [
                        'device_type' => 'v_whty_1',
                        'device_name' => '播报设备(天喻东莞)'
                    ],
                    [
                        'device_type' => 'v_ld_ns20',
                        'device_name' => '播报设备(兴业银行云喇叭)'
                    ]
                    ,[
                        'device_type' => 'csyz_YZ07',
                        'device_name' => '传盛语智YZ07'
                    ]
                ];
            }
            if ($type == "x") {
                $data = [
                    [
                        'device_type' => 'x_haidian',
                        'device_name' => '收银系统(海典)'
                    ],
                    [
                        'device_type' => 'face_f4',
                        'device_name' => '收银系统(千禾)'
                    ],
                    [
                        'device_type' => 'hd123',
                        'device_name' => '收银系统(海鼎)'
                    ],
                    [
                        'device_type' => 'x_biash',
                        'device_name' => '收银系统(爱宝软件)'
                    ],
                    [
                        'device_type' => 'x_sixun',
                        'device_name' => '收银系统(思迅)'
                    ],
                    [
                        'device_type' => 'x_kylinos',
                        'device_name' => '收银系统(麒麟软件)'
                    ]
                    ,
                    //
                    [
                        'device_type' => 'x_km',
                        'device_name' => '收银系统(科脉)'
                    ],
                    [
                        'device_type' => 'x_hgj',
                        'device_name' => '收银系统(惠管家)'
                    ],
                    [
                        'device_type' => 'x_zbw',
                        'device_name' => '收银系统(智百威)'
                    ],
                    [
                        'device_type' => 'x_zl',
                        'device_name' => '收银系统(查亮接口)'
                    ],
                    [
                        'device_type' => 'x_hsx',
                        'device_name' => '收银系统(慧算星)'
                    ],
                    [
                        'device_type' => 'x_yb',
                        'device_name' => '收银系统(银豹)'
                    ],
                    [
                        'device_type' => 'x_kwy',
                        'device_name' => '收银系统(客无优)'
                    ],
                    [
                        'device_type' => 'x_jlk',
                        'device_name' => '收银系统(聚来客)'
                    ],
                    [
                        'device_type' => 'x_by',
                        'device_name' => '收银系统(博优)'
                    ],
                    [
                        'device_type' => 'x_kmtk',
                        'device_name' => '收银系统(康明泰康)'
                    ],
                    [
                        'device_type' => 'x_lm',
                        'device_name' => '收银系统(乐檬)'
                    ],
                    [
                        'device_type' => 'x_nk',
                        'device_name' => '收银系统(纳客)'
                    ],
                    [
                        'device_type' => 'x_zssz',
                        'device_name' => '收银系统(中市数智)'
                    ]
                ];
            }

            $data = DB::table('device_list')->where(['type' => $type])->get();

            $this->status = 1;
            $this->message = "数据返回成功";

            return $this->format($data);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => '-1',
                'message' => $exception->getMessage()
            ]);
        }
    }


    public function get_device(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();

            $data = $request->except(['token']);
            $check_data = [
                'type' => '设备类型',
                'name' => '收件人名字',
                'phone' => '收件人电话',
                'address' => '详细地址',
                'province_code' => '省',
                'city_code' => '市',
                'area_code' => '区'
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $this->status = 1;
            $this->message = "申请成功";
            return $this->format($data);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage()
            ]);
        }
    }


    //配置
    public function v_config(Request $request)
    {
        try {
            $user_merchant = $this->parseToken();
            $config_id = $user_merchant->config_id;
            $zlbz_token = $request->get('zlbz_token', "");
            $zw_token = $request->input('zw_token', "");
            $yly_user_name = $request->post('yly_user_name', "");
            $yly_user_id = $request->post('yly_user_id', "");
            $yly_api_key = $request->post('yly_api_key', "");
            $type = $request->post('type', ""); //有值为更新
            $tencent_cloud_secretid = $request->post('tencent_cloud_secretid', "");
            $tencent_cloud_secretkey = $request->post('tencent_cloud_secretkey', "");
            $cls_prefix = $request->post('cls_prefix', "");
            $cls_product_id = $request->post('cls_product_id', "");

            $VConfig = VConfig::where('config_id', $config_id)->first();

            if (!$type) {
                $this->status = 1;
                $this->message = "查询成功";
                return $this->format($VConfig);
            }

            $data = [
                'config_id' => $config_id,
                'zlbz_token' => $zlbz_token,
                'zw_token' => $zw_token,
                'yly_user_name' => $yly_user_name,
                'yly_user_id' => $yly_user_id,
                'yly_api_key' => $yly_api_key,
                'tencent_cloud_secretid' => $tencent_cloud_secretid ?? '',
                'tencent_cloud_secretkey' => $tencent_cloud_secretkey ?? '',
                'cls_prefix' => $cls_prefix ?? '',
                'cls_product_id' => $cls_product_id ?? '',
            ];
            if ($VConfig) {
                $VConfig->update($data);
                $VConfig->save();
            } else {
                VConfig::create($data);
            }

            $this->status = 1;
            $this->message = "添加成功";
            return $this->format($data);
        } catch (\Exception $ex) {
            Log::info('添加设备-error');
            Log::info($ex->getMessage() . ' | ' . $ex->getFile() . ' | ' . $ex->getLine());
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage() . ' | ' . $ex->getFile() . ' | ' . $ex->getLine()
            ]);
        }
    }

    public function feieyun($url, $data)
    {
        $querystring = '';
        if (is_array($data)) {
            foreach ($data as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $val2) {
                        $querystring .= urlencode($key) . '=' . urlencode($val2) . '&';
                    }
                } else {
                    $querystring .= urlencode($key) . '=' . urlencode($val) . '&';
                }
            }
            $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
        } else {
            $querystring = $data;
        }

        // $curl = curl_init();
        // curl_setopt($curl, CURLOPT_URL, $url);
        // curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据
        // curl_setopt($curl, CURLOPT_TIMEOUT, (int)5000);//超时设置
        // curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值
        // curl_setopt($curl, CURLOPT_POST, 1);
        // curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点
        // curl_setopt($curl, CURLOPT_POSTFIELDS, $querystring);
        // $res = curl_exec($curl);
        $res = '{"msg":"ok","ret":0,"data":{"ok":["223510102#kfn9r4fn"],"no":[]},"serverExecutedTime":19}';
        return $res;
    }


}
